<?php

error_reporting(E_ALL);
/* 应用根目录 */
define('APP_ROOT', dirname(__FILE__));     //该常量只在后台使用

define('ROOT_PATH', dirname(APP_ROOT));   //网站根目录
//echo ROOT_PATH;die();
set_time_limit(500);

if (!empty($_GET['act']) && $_GET['act'] == 'create')
{

    $html_content = 'http://' . $_SERVER ['HTTP_HOST'] . '/index.php?act=make_index';

    $file_html_content = file_get_contents($html_content);
    //echo $file_html_content;die();

    $filename = ROOT_PATH . "/" . 'index.html'; //生成后的静态文件目录，绝对路径

    make_to_html($filename, $file_html_content);
}

if (!empty($_GET['act']) && $_GET['act'] == 'sitemap')
{
    $html_sitemap = 'http://' . $_SERVER ['HTTP_HOST'] . '/index.php?app=sitemap&act=make_html&do=htmlnx';
    $xml_sitemap = 'http://' . $_SERVER ['HTTP_HOST'] . '/index.php?app=sitemap&act=make_xml&do=xmlnx';

    $html_content = file_get_contents($html_sitemap);
    $xml_content = file_get_contents($xml_sitemap);
    //echo $file_html_content;die();

    $html_filename = ROOT_PATH . "/" . 'sitemap.html'; //生成后的静态文件目录，绝对路径
    $xml_filename = ROOT_PATH . "/" . 'sitemap.xml';
    
    make_to_html($html_filename, $html_content);
    make_to_html($xml_filename, $xml_content);
}

//把生成文件的过程写出函数
function make_to_html($file_html_name, $file_html_content)
{

    if (is_file($file_html_name))
    {
        @unlink($file_html_name);
    }
    $cjjer_handle = fopen($file_html_name, "w");
    if (!is_writable($file_html_name))
    {
        //return false;
        echo '没有权限！';
        exit;
    }
    if (!fwrite($cjjer_handle, $file_html_content))
    {
        echo '生成失败！';
        exit;
    }
    fclose($cjjer_handle); //关闭指针
    echo '生成成功！';
}

?>